package cn.wgx.modules.sys.util;

import cn.wgx.commons.util.SpringContextHolder;
import cn.wgx.commons.util.StringUtils;
import cn.wgx.commons.util.Validation;
import cn.wgx.modules.sys.entity.SysDict;
import cn.wgx.modules.sys.services.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * 字典工具类
 */
public class DictUtil {

    /**
     * 字典类型常量 - 团队
     */
    public static final String TYPE_TEAM = "TYPE_TEAM";
    /**
     * 字典类型常量 - 部门
     */
    public static final String TYPE_DEPARTMENT = "TYPE_DEPARTMENT";
    /**
     * 字典类型常量 - 项目管理
     */
    public static final String TYPE_MANAGE = "TYPE_MANAGER_";
    /**
     * 字典类型常量 - 项目参数（项目设置中）
     */
    public static final String TYPE_PARAMETER = "TYPE_PARAMETER_";


    @Autowired
    private static SysDictService sysDictService;

    static{
        if(sysDictService == null){
            sysDictService = SpringContextHolder.getBean("sysDictService");
        }
    }

    public static SysDict getSysDictByDictId(Integer dict_id){
        if(Validation.isNullOrZero(dict_id)){
            return null;
        }
        SysDict sysDict1 = new SysDict();
        sysDict1.setDict_id(dict_id);
        List<SysDict> sysDicts = sysDictService.getList( sysDict1,null,null);
        if(sysDicts != null && !sysDicts.isEmpty()){
            return sysDicts.get(0);
        }
        return null;
    }


    public static SysDict getValueByKey(String type, String key){
        if(StringUtils.isBlank(type)|| StringUtils.isBlank(key)){
            return null;
        }
        SysDict sysDict1 = new SysDict();
        sysDict1.setType(type);
        sysDict1.setK(key);
        List<SysDict> sysDicts = sysDictService.getList( sysDict1,null,null);
        if(sysDicts != null && !sysDicts.isEmpty()){
            return sysDicts.get(0);
        }
        return null;
    }

    public static SysDict getSysDictById(String type, Integer dictId){
        if(StringUtils.isBlank(type)|| Validation.isNullOrZero(dictId)){
            return null;
        }
        SysDict sysDict1 = new SysDict();
        sysDict1.setType(type);
        sysDict1.setDict_id(dictId);
        List<SysDict> sysDicts = sysDictService.getList( sysDict1,null,null);
        if(sysDicts != null && !sysDicts.isEmpty()){
            return sysDicts.get(0);
        }return null;
    }

    public static int insertValueByType(SysDict sysDict){
        return sysDictService.insertOne(sysDict);
    }

    public static List<SysDict> insertValueByType(String type, Map<String,String> map){
        ArrayList<SysDict> sysDicts = new ArrayList<>();
        for(Map.Entry<String, String> entry: map.entrySet()){
            sysDicts.add(createNewDict(type,entry.getKey(),entry.getValue(),0));
        }
        return sysDictService.insertList(sysDicts);
    }

    public static int updateByDictId(Integer id, SysDict sysDict){
        sysDict.setDict_id(id);
        return sysDictService.updateOne(sysDict);
    }

    public static int updateByDictIdAndType(SysDict sysDict){
        return sysDictService.updateByIDAndType(sysDict);
    }

    public static int updateByTypeAndKey(String type, Map<String, Object> map){
        int r = 0;
        for(Map.Entry<String, Object> entry: map.entrySet()){
            r+=updateByTypeAndKey(type, entry.getKey(), entry.getValue()==null?"":entry.getValue().toString());
        }
        return r;
    }

    public static int updateByTypeAndKey(String type, String key, String v){
        SysDict valueByKey = getValueByKey(type, key);
        if(valueByKey == null){
            SysDict sysDict = new SysDict();
            sysDict.setType(type);
            sysDict.setK(key);
            sysDict.setV(v);
            return insertValueByType(sysDict);
        }else return sysDictService.updateByTypeAndKey(type, key, v);
    }

    public static  List<SysDict> getListByParentId( Integer parentId){
        SysDict sysDict1 = new SysDict();
        sysDict1.setParentId(parentId);
        List<SysDict> sysDicts = sysDictService.getList( sysDict1,"sort","asc");
        return sysDicts;
    }

    public static  List<SysDict> getListByTypeAndParentId(String type, Integer parentId){
        SysDict sysDict1 = new SysDict();
        sysDict1.setType(type);
        sysDict1.setParentId(parentId);
        List<SysDict> sysDicts = sysDictService.getList( sysDict1,"sort","asc");
        return sysDicts;
    }

    public static  List<SysDict> getListByParentKey(String type, String parentKey){
        SysDict sysDict1 = new SysDict();
        sysDict1.setType(type);
        sysDict1.setParentKey(parentKey);
        List<SysDict> sysDicts = sysDictService.getList(sysDict1,"sort","asc");
        return sysDicts;
    }

    public static  List<SysDict> getListByType(String type){
        SysDict sysDict1 = new SysDict();
        sysDict1.setType(type);
        List<SysDict> sysDicts = sysDictService.getList( sysDict1,"sort","asc");
        return sysDicts;
    }

    public static  List<SysDict> getListByEntity(SysDict SysDict){
        return getListByEntity(SysDict, "sort", "asc");
    }


    public static  List<SysDict> getListByEntity(SysDict SysDict, String order, String asc){
        List<SysDict> sysDicts = sysDictService.getList( SysDict, order, asc);
        return sysDicts;
    }


    public static  List<SysDict> getListByEntity(SysDict SysDict, String order, String asc, String extendSQL){
        List<SysDict> sysDicts = sysDictService.getList( SysDict, order, asc, extendSQL);
        return sysDicts;
    }

    public static Map<String,String> getValuesByType(String type){
        SysDict sysDict1 = new SysDict();
        sysDict1.setType(type);
        Map map = new HashMap();
        List<SysDict> sysDicts = sysDictService.getList( sysDict1,null,null);
        if(sysDicts != null && sysDicts.size() > 0){
            for(SysDict sysDict: sysDicts){
                map.put(sysDict.getK(), sysDict.getV());
            }
            return  map;
        }
        return map;
    }



    public static SysDict createNewDict(String type, String k, String v, Integer sort, Integer parentId, String parentKey, String extendValue){
        SysDict sysDict = new SysDict();
        sysDict.setType(type);
        sysDict.setK(k);
        sysDict.setV(v);
        sysDict.setParentKey(parentKey);
        sysDict.setParentId(parentId);
        sysDict.setSort(sort);
        sysDict.setExtendValue(extendValue);
        return sysDict;
    }

    public static SysDict createNewDict(String type, String k, String v, Integer sort){
        return createNewDict(type, k, v,  sort,null, null,null);
    }

    public static SysDict createNewDict(String type, String k, String v, Integer sort , String parentKey){
        return createNewDict(type, k, v, sort, null, parentKey, null);
    }
    public static SysDict createNewDict(String type, String k, String v, Integer sort , Integer parentId){
        return createNewDict(type, k, v, sort, parentId, null,null);
    }

    public static SysDict createNewDict(String type, String k, String v, Integer sort, Integer parentId, String extendValue){
        return createNewDict(type, k, v, sort, parentId, null, extendValue);
    }


    public static int delById(Integer id){
        return sysDictService.deleteOne(id);
    }

    public static int delByType(String type){
        return sysDictService.deleteByType(type);
    }

    public static int delByTypeAndId(String type, Integer id){
        return sysDictService.deleteByTypeAndId(type, id);
    }

    public static void clearCache(){
        sysDictService.deleteOne(0);
    }

    public static void clearCache(String type){
        sysDictService.deleteOne(0);
    }
}
